package com.gitee.lw114514.tliasweb.controller;

import com.gitee.lw114514.tliasweb.pojo.Emp;
import com.gitee.lw114514.tliasweb.pojo.EmpQueryParam;
import com.gitee.lw114514.tliasweb.pojo.PageResult;
import com.gitee.lw114514.tliasweb.pojo.Result;
import com.gitee.lw114514.tliasweb.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/*
 * 员工管理的controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    /*
     * 分页查询员工信息
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam
//            @RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10") Integer pageSize,
//                       String name,
//                       Integer gender,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        Integer page=empQueryParam.getPage();
        Integer pageSize=empQueryParam.getPageSize();
        String name=empQueryParam.getName();
        Integer gender=empQueryParam.getGender();
        LocalDate begin=empQueryParam.getBegin();
        LocalDate end=empQueryParam.getEnd();

        log.info("分页查询员工信息，页码：{}，每页记录数：{}，姓名：{}，性别：{}," +
                "入职时间：{}，更新时间：{}", page, pageSize, name, gender, begin, end);
        PageResult<Emp> pageResult =empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /*
    * 新增员工
     */
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工，员工数据：{}", emp);
        empService.save(emp);
        return Result.success();
    }

    /*
     * 批量删除员工
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工，员工id：{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    /*
     * 根据员工id查询员工
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("修改查询回显，员工id：{}", id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    /*
     * 修改员工信息
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工信息：{}", emp);
        empService.update(emp);
        return Result.success();
    }

    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询全部员工信息");
        List<Emp> empList = empService.listAll();
        return Result.success(empList);
    }
}
